package co.fitstart.admin.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;

import co.fitstart.admin.dao.CouponDao;
import co.fitstart.admin.dao.CreditCouponBatchDao;
import co.fitstart.admin.dao.UserDao;
import co.fitstart.admin.entity.content.CreditCouponBatchSearch;
import co.fitstart.admin.service.CreditCouponBatchService;
import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.CouponBatch;
import co.fitstart.entity.coupon.CreditCouponBatch;
import co.fitstart.entity.coupon.ExpireType;
import co.fitstart.entity.coupon.stat.CouponTemplateStat;
import co.fitstart.entity.user.User;

/**
 * 
 * @author Ysy
 *
 */
@Service("creditCouponBatchService")
public class CreditCouponBatchServiceImpl extends DataServiceImpl<CreditCouponBatch> implements CreditCouponBatchService {

    @Dynamic
    private CreditCouponBatchDao creditCouponBatchDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dynamic
    private CouponDao couponDao;
    
    @Dynamic
    private UserDao userDao;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CreditCouponBatchService#list(com.demo2do.core.support.Page)
     */
    public List<CreditCouponBatch> list(Page page) {
        List<CreditCouponBatch> result = creditCouponBatchDao.list(page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CreditCouponBatchService#search(co.fitstart.admin.entity.content.CreditCouponBatchSearch, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<CreditCouponBatch> search(CreditCouponBatchSearch creditCouponBatchSearch, Page page) {
        String sentence = creditCouponBatchSearch.buildSentenceTemplate();
        Map<String, Object> parameters = creditCouponBatchSearch.buildParameters();
        List<CreditCouponBatch> result = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CreditCouponBatchService#create(co.fitstart.entity.coupon.CreditCouponBatch)
     */
    public void create(CreditCouponBatch creditCouponBatch) {
        
        //1.create a new CouponBatch by creditCouponBatch
        CouponBatch couponBatch = new CouponBatch();
        genericDaoSupport.save(couponBatch.initialize(creditCouponBatch).initialize(creditCouponBatch.getCouponTemplate()));
        
        //2.save creditCouponBatch by CouponBatch
        genericDaoSupport.save(creditCouponBatch.initialize(couponBatch));
        
        //3.修改couponTemplateStat
        Long couponTemplateId = couponBatch.getCouponTemplate().getId();
        CouponTemplateStat couponTemplateStat = couponDao.getCouponTemplateStat(couponTemplateId);
        genericDaoSupport.update(couponTemplateStat.edit(couponBatch));
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CreditCouponBatchService#edit(co.fitstart.entity.coupon.CreditCouponBatch, co.fitstart.entity.coupon.ExpireType)
     */
    public void edit(CreditCouponBatch creditCouponBatch, ExpireType inputExpireType) {
        CouponBatch couponBatch = genericDaoSupport.load(CouponBatch.class, creditCouponBatch.getCouponBatch().getId());
        
        if(!(inputExpireType.equals(creditCouponBatch.getExpireType()))){
            if(inputExpireType.equals(ExpireType.FIXED_DATE)){
                couponBatch.editEffectiveDays(0);
                creditCouponBatch.editEffectiveDays(0);
            }
            else if(inputExpireType.equals(ExpireType.FIXED_DAYS)){
                couponBatch.editEffectiveDate();
                creditCouponBatch.editEffectiveDate();
            }
               
        }
        
        genericDaoSupport.update(couponBatch.editExpireType(inputExpireType));
        genericDaoSupport.update(creditCouponBatch.editExpireType(inputExpireType));
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CreditCouponBatchService#generateCoupon(co.fitstart.entity.coupon.CreditCouponBatch)
     */
    public void generateCoupon(CreditCouponBatch creditCouponBatch) {
        
        CouponBatch couponBatch = creditCouponBatch.getCouponBatch();
        int restCount = creditCouponBatch.getAmount() - creditCouponBatch.getReleaseAmount();
        
        for (int i = 0; i < restCount; i++) {
            genericDaoSupport.save(new Coupon(couponBatch));
        }
        
        genericDaoSupport.update(creditCouponBatch.editReleaseAmount(restCount));
        genericDaoSupport.update(couponBatch.editReleaseAmount(restCount));
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.CreditCouponBatchService#identify(co.fitstart.entity.coupon.Coupon, java.lang.String)
     */
    public void identify(Coupon coupon, String identification) {
        // 1. 匹配identification 与 user的手机号。返回user
        User user = userDao.load(identification);
        
        // 将user赋给coupon的user,把页面传进来的identification参数 update给coupon
        genericDaoSupport.update(coupon.identify(user.getId(), identification));
        
        // update coupon batch's claimed count
        CouponBatch couponBatch = coupon.getBatch();
        genericDaoSupport.update(couponBatch.editClaimedAmount());
        genericDaoSupport.update(creditCouponBatchDao.loadByCouponBatch(couponBatch).editClaimedAmount());
        
    }

	/*
	 * (non-Javadoc)
	 * @see co.fitstart.admin.service.CreditCouponBatchService#list()
	 */
	public List<CreditCouponBatch> list() {
		return creditCouponBatchDao.list();
	}
}
